package com.yy.controller.restaurantmanager;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yy.common.core.AjaxResult;
import com.yy.common.core.HttpStatus;
import com.yy.common.core.model.SurveyVM;
import com.yy.common.core.page.TableDataInfo;
import com.yy.common.utils.PageHelper;
import com.yy.pojo.Restaurant;
import com.yy.pojo.Survey;
import com.yy.pojo.SurveyItem;
import com.yy.service.RestaurantService;
import com.yy.service.SurveyItemService;
import com.yy.service.SurveyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("restaurant-manager/vote")
public class VoteController {

    @Autowired
    private SurveyService surveyService;
    @Autowired
    private SurveyItemService surveyItemService;

    @Autowired
    private RestaurantService restaurantService;

    @GetMapping("/query")
    public TableDataInfo querySurveyList(@RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                         @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {

        IPage<Survey> page = surveyService.page(new Page<>(pageNum, pageSize));
        List<Restaurant> restaurantList = restaurantService.list();
        Page<SurveyVM> surveyVMPage = PageHelper.copyMap(page, s -> {
            SurveyVM surveyVM = new SurveyVM();
            BeanUtil.copyProperties(s, surveyVM);
            restaurantList.stream().filter(r -> s.getRestaurantId().equals(r.getRestaurantId()))
                    .findFirst().ifPresent(r -> {
                                surveyVM.setRestaurantName(r.getRestaurantName());
                            }
                    );
            surveyVM.setCreateTime(DateUtil.format(s.getCreateTime(), "yyyy年MM月dd日 HH:mm:ss"));
            return surveyVM;
        });
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(surveyVMPage.getRecords());
        rspData.setTotal(surveyVMPage.getTotal());
        return rspData;

    }

    @PostMapping("/add")
    public AjaxResult addSurvey(@RequestPart("survey") Survey survey,
                                @RequestPart("sub") List<SurveyItem> surveyItems) {
        survey.setCreateTime(DateUtil.date());
        boolean surveySava = surveyService.save(survey);

        if (surveySava) {
            surveyItems.forEach(s -> {
                s.setSurveyId(survey.getSurveyId());
                s.setCreateTime(DateUtil.date());
            });
            boolean surveyItemsSave = surveyItemService.saveBatch(surveyItems);
            if (surveyItemsSave) {
                return AjaxResult.success();
            } else {
                surveyService.removeById(survey);
            }
        }
        return AjaxResult.error();
    }

    @DeleteMapping("/delete/{id}")
    public AjaxResult deleteSurvey(@PathVariable("id") Long id) {
        boolean remove = surveyService.removeById(id);
        if (remove) {
            surveyItemService.remove(Wrappers.lambdaQuery(SurveyItem.class).eq(SurveyItem::getSurveyId, id));
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    @GetMapping("{id}")
    public AjaxResult querySurveyItem(@PathVariable("id") Long id) {
        return AjaxResult.success(surveyItemService.list(Wrappers.lambdaQuery(SurveyItem.class)
                .eq(SurveyItem::getSurveyId, id)));
    }
}


// 增加点代码行数，显得我没这么划水
